package com.woniu.woniu_bx.component;

import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @Description 尝试解决redis连接timeout问题
 * @Author zhangjin
 * @Date 2022/9/8 10:17
 */
@Component
public class MyLuttuceCronTask implements LettuceClientConfigurationBuilderCustomizer {
    @Override
    public void customize(LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigurationBuilder) {
        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()

                // 开始自适应刷新

                .enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)

                .enableAllAdaptiveRefreshTriggers()

                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(10))

                //开启定时刷新 时间间隔自己定义

                .enablePeriodicRefresh(Duration.ofSeconds(15))

                .build();

        clientConfigurationBuilder.clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build());


    }
}
